import _ from './rng.js';
import { unsafeStringify as g } from './stringify.js';
let q,
  a,
  m = 0,
  i = 0;
function S(f, u, w) {
  let c = (u && w) || 0;
  const n = u || new Array(16);
  f = f || {};
  let d = f.node || q,
    l = f.clockseq !== void 0 ? f.clockseq : a;
  if (d == null || l == null) {
    const e = f.random || (f.rng || _)();
    d == null && (d = q = [e[0] | 1, e[1], e[2], e[3], e[4], e[5]]),
      l == null && (l = a = ((e[6] << 8) | e[7]) & 16383);
  }
  let s = f.msecs !== void 0 ? f.msecs : Date.now(),
    t = f.nsecs !== void 0 ? f.nsecs : i + 1;
  const k = s - m + (t - i) / 1e4;
  if (
    (k < 0 && f.clockseq === void 0 && (l = (l + 1) & 16383),
    (k < 0 || s > m) && f.nsecs === void 0 && (t = 0),
    t >= 1e4)
  )
    throw new Error("uuid.v1(): Can't create more than 10M uuids/sec");
  (m = s), (i = t), (a = l), (s += 122192928e5);
  const r = ((s & 268435455) * 1e4 + t) % 4294967296;
  (n[c++] = (r >>> 24) & 255),
    (n[c++] = (r >>> 16) & 255),
    (n[c++] = (r >>> 8) & 255),
    (n[c++] = r & 255);
  const x = ((s / 4294967296) * 1e4) & 268435455;
  (n[c++] = (x >>> 8) & 255),
    (n[c++] = x & 255),
    (n[c++] = ((x >>> 24) & 15) | 16),
    (n[c++] = (x >>> 16) & 255),
    (n[c++] = (l >>> 8) | 128),
    (n[c++] = l & 255);
  for (let e = 0; e < 6; ++e) n[c + e] = d[e];
  return u || g(n);
}
export { S as default };
